#!/bin/bash
trap 'echo Abort ; exit 1' ERR
err() { echo "Error. $@ not set."; exit 1; }
. .varlist
[ -z "$TOOLCHAIN_DIR" ] && err TOOLCHAIN_DIR
[ -z "$GLIBC" ] && err GLIBC
[ -z "$TARGET" ] && err TARGET
[ -z "$BUILD_DIR" ] && err BUILD_DIR
[ -z "$SOURCE_DIR" ] && err SOURCE_DIR
[ -z "$HOST" ] && err HOST
[ -z "$GLIBC_PORTS" ] && err GLIBC_PORTS

echo Build glibc.
[ -f $TOOLCHAIN_DIR/.$GLIBC ] && exit 0
export PATH=$PATH:$TOOLCHAIN_DIR/usr/bin/ #:$TOOLCHAIN_DIR/usr/$TARGET/bin/

rm $BUILD_DIR/$GLIBC -rf
mkdir $BUILD_DIR/$GLIBC
cd $BUILD_DIR/$GLIBC
[ -d $SOURCE_DIR/$GLIBC/ports ] || cp $SOURCE_DIR/$GLIBC_PORTS $SOURCE_DIR/$GLIBC/ports -r

echo "install_root=$TOOLCHAIN_DIR" > configparms

libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes libc_cv_gnu99_inline=yes \
BUILD_CC=gcc CC=${TARGET}-gcc AR=${TARGET}-ar RANLIB=${TARGET}-ranlib \
$SOURCE_DIR/$GLIBC/configure \
	--prefix=/usr --build=$HOST --host=$TARGET \
	--disable-profile --enable-add-ons --with-tls \
	--enable-kernel=2.6.28 --with-__thread \
	--with-binutils=$TOOLCHAIN_DIR/usr/bin \
	--with-headers=$TOOLCHAIN_DIR/usr/include \
	--libexecdir=/usr/lib/glibc > /dev/null
set -x
make > /dev/null
make install_root=$TOOLCHAIN_DIR install > /dev/null

touch $TOOLCHAIN_DIR/.$GLIBC
